package net.sourceforge.jradiusclient.attributes;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import net.sourceforge.jradiusclient.RadiusAttribute;
import net.sourceforge.jradiusclient.RadiusAttributeValues;
import net.sourceforge.jradiusclient.exception.InvalidParameterException;

/**
 * 
 * @author Yifu.HUANG
 * 
 */
public class VendorSpecificAttribute extends RadiusAttribute {
	public static final int VENDOR_ID_ALCATEL = 2323;

	byte[] vendorValue = null;

	public byte[] getVendorValue() {
		return vendorValue;
	}

	int vendorId;

	public int getVendorId() {
		return vendorId;
	}

	int vendorType;

	public int getVendorType() {
		return vendorType;
	}

	public VendorSpecificAttribute(int vendorId, int vendorType,
			byte[] vendorValue) throws InvalidParameterException {
		super(RadiusAttributeValues.VENDOR_SPECIFIC, getVsaBytes(vendorId,
				vendorType, vendorValue));
		this.vendorValue = vendorValue;
		this.vendorId = vendorId;
		this.vendorType = vendorType;
	}

	// public VendorSpecificAttribute(int vendorId, int vendorType, int
	// vendorValue)
	// throws InvalidParameterException {
	// super(RadiusAttributeValues.VENDOR_SPECIFIC, getVsaBytes(vendorId,
	// vendorType, vendorValue));
	//		
	// }
	//
	// public VendorSpecificAttribute(int vendorId, int vendorType,
	// short vendorValue) throws InvalidParameterException {
	// super(RadiusAttributeValues.VENDOR_SPECIFIC, getVsaBytes(vendorId,
	// vendorType, vendorValue));
	// }
	//
	// public VendorSpecificAttribute(int vendorId, int vendorType,
	// byte vendorValue) throws InvalidParameterException {
	// super(RadiusAttributeValues.VENDOR_SPECIFIC, getVsaBytes(vendorId,
	// vendorType, vendorValue));
	// }

	public VendorSpecificAttribute(int vendorType, byte[] vendorValue)
			throws InvalidParameterException {
		super(RadiusAttributeValues.VENDOR_SPECIFIC, getVsaBytes(
				VENDOR_ID_ALCATEL, vendorType, vendorValue));
		this.vendorValue = vendorValue;
		this.vendorId = VENDOR_ID_ALCATEL;
		this.vendorType = vendorType;
	}

	// public VendorSpecificAttribute(int vendorType, int vendorValue)
	// throws InvalidParameterException {
	// super(RadiusAttributeValues.VENDOR_SPECIFIC, getVsaBytes(
	// VENDOR_ID_ALCATEL, vendorType, vendorValue));
	// }
	//
	// public VendorSpecificAttribute(int vendorType, short vendorValue)
	// throws InvalidParameterException {
	// super(RadiusAttributeValues.VENDOR_SPECIFIC, getVsaBytes(
	// VENDOR_ID_ALCATEL, vendorType, vendorValue));
	// }
	//
	// public VendorSpecificAttribute(int vendorType, byte vendorValue)
	// throws InvalidParameterException {
	// super(RadiusAttributeValues.VENDOR_SPECIFIC, getVsaBytes(
	// VENDOR_ID_ALCATEL, vendorType, vendorValue));
	// }

	// public static byte[] getVsaBytes(int vendorId, int vendorType,
	// int vendorValue) throws InvalidParameterException {
	// return getVsaBytes(vendorId, vendorType, new byte[] {
	// (byte) (vendorValue >>> 24 & 0xFF),
	// (byte) (vendorValue >>> 16 & 0xFF),
	// (byte) (vendorValue >>> 8 & 0xFF),
	// (byte) (vendorValue >>> 0 & 0xFF) });
	// }
	//
	// public static byte[] getVsaBytes(int vendorId, int vendorType,
	// short vendorValue) throws InvalidParameterException {
	// return getVsaBytes(vendorId, vendorType, new byte[] {
	// (byte) (vendorValue >>> 8 & 0xFF),
	// (byte) (vendorValue >>> 0 & 0xFF) });
	// }
	//
	// public static byte[] getVsaBytes(int vendorId, int vendorType,
	// byte vendorValue) throws InvalidParameterException {
	// return getVsaBytes(vendorId, vendorType,
	// new byte[] { (byte) (vendorValue >>> 0 & 0xFF) });
	// }

	public static byte[] getVsaBytes(int vendorId, int vendorType,
			byte[] vendorValue) throws InvalidParameterException {
		byte[] rtn = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream output = new DataOutputStream(baos);
		try {
			output.writeInt(vendorId);
			output.writeByte(vendorType);
			output.writeByte(vendorValue.length + 2);
			output.write(vendorValue);
			output.flush();
			rtn = baos.toByteArray();
			output.close();
			baos.close();
		} catch (IOException ioex) {// this should never happen
			throw new InvalidParameterException(
					"Error constructing RadiusAttribute");
		}
		return rtn;
	}
	//
	// public static void main(String[] args) throws InvalidParameterException {
	// byte[] bs = getVsaBytes(2323, 6, "a".getBytes());
	// for (int i = 0; i < bs.length; i++) {
	// System.out.print(Integer.toHexString(bs[i]) + " ");
	// }
	// }
}
